import Feature from "ol/Feature";
import LineString from "ol/geom/LineString";
import Style from "ol/style/Style";
import Stroke from "ol/style/Stroke";
import Polygon from "ol/geom/Polygon";

function Polyline(map) {
  this.map = map;
}

/**
 * @description: 添加线
 * @param {*} data 线坐标
 * @param {*} layer 图层对象
 * @param {*} styleObj 样式对象 {strokeColor:'',strokeWidth:''}
 * @return {*}
 */
Polyline.prototype.addPolyline = function (data, layer, styleObj) {
  let polylineLayer = layer;
  //实例化Vector要素，通过矢量图层添加到地图容器中
  let feature = new Feature({
    geometry: new LineString(data, "XY"),
  });
  feature.setStyle(styleFunction(styleObj));
  let source = polylineLayer.getSource();
  source.addFeature(feature);
};

Polyline.prototype.addPolylineByGeom = function (geometry, layer, styleObj) {
  let polylineLayer = layer;
  let feature = new Feature({
    geometry: new Polygon(geometry),
  });
  feature.setStyle(styleFunction(styleObj));
  let source = polylineLayer.getSource();
  source.addFeature(feature);
};

let styleFunction = function (styleObj) {
  const style = new Style({
    stroke: new Stroke({
      color: styleObj.strokeColor || "#FFFFFF",
      width: styleObj.strokeWidth || 2,
      //lineDash:[1,2,3,4,5,6]
    }),
  });
  return style;
};

export default Polyline;
